#include <jni.h>
#include <string>

extern "C" {
extern int patch_main(int argc, char *argv[]);
extern int diff_main(int argc,char *argv[]);
JNIEXPORT
jint JNICALL
Java_native_BspatchUtil_00024Companion_patch(JNIEnv *env, jobject thiz, jstring path_old,
                                             jstring path_new,
                                             jstring patch) {
    const char* old_path = env->GetStringUTFChars(path_old,0);
    const char * new_path = env->GetStringUTFChars(path_new,0);
    const char * patch_path = env->GetStringUTFChars(patch,0);
    int argc = 4;//参数个数
    char *argv[argc];//参数列表
    //  ./bspatch 1.apk new.apk 1-2.patch
    argv[0] = "bspatch";
    argv[1] = const_cast<char *>(old_path);
    argv[2] = const_cast<char *>(new_path);
    argv[3] = const_cast<char *>(patch_path);
    int result = patch_main(argc,argv);
    env->ReleaseStringUTFChars(path_old, old_path);
    env->ReleaseStringUTFChars(path_new, new_path);
    env->ReleaseStringUTFChars(patch, patch_path);
    return result;//0表示成功
}
}
extern "C"
JNIEXPORT jint JNICALL
Java_native_BspatchUtil_00024Companion_diff(JNIEnv *env, jobject thiz, jstring path_old,
                                            jstring path_new, jstring patch) {
    int argc = 4;//参数个数
    //./bsdiff 1.apk 2.apk 1-2.patch
    char* argv[argc];//参数列表
    //  ./bspatch 1.apk new.apk 1-2.patch
    argv[0] = "bsdiff";
    argv[1] = const_cast<char *>(env->GetStringUTFChars(path_old,0)) ;
    argv[2] = const_cast<char *>(env->GetStringUTFChars(path_new,0));
    argv[3] = const_cast<char *>(env->GetStringUTFChars(patch,0));
    int result = diff_main(argc,argv);
    env->ReleaseStringUTFChars(path_old,argv[1]);
    env->ReleaseStringUTFChars(path_new,argv[2]);
    env->ReleaseStringUTFChars(patch,argv[3]);
    return result;//0表示成功
}